package com.gtw.modules.api.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.gtw.common.annotation.AuthIgnoreApi;
import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.*;
import com.gtw.dto.*;
import com.gtw.modules.api.service.*;
import com.gtw.modules.dao.TbActivityCommodityDao;
import com.gtw.modules.dao.TbActivityDao;
import com.gtw.modules.dao.TbActivityUserDao;
import com.gtw.modules.dao.TbBargainDao;
import com.gtw.modules.entity.*;
import com.gtw.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 活动
 *
 * @author wuye
 * @email www.baidu.com
 * @date 2018-02-06 11:38:12
 */
@RestController
@RequestMapping("/api/acticity")
public class TbActivityController {

    @Autowired
    private TbActivityServices tbActivityServices;

    @Autowired
    private TbActivityCommodityDao tbActivityCommodityDao;

    @Autowired
    private TbCommodityService tbCommodityService;

    @Autowired
    private TbCommodityPropertyService tbCommodityPropertyService;

    @Autowired
    private TbPropertyService tbPropertyService;

    @Autowired
    private TbActivityUserDao tbActivityUserDao;

    @Autowired
    private TbBargainDao tbBargainDao;

    @Autowired
    private TbBargainService tbBargainService;

    @Autowired
    private TbTokenService tokenService;

    @Autowired
    private TbUserService tbUserService;

    /**
     * 查询列表
     *
     * @param params
     * @returnapi
     */
    @AuthIgnoreApi
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        Page<TbActivity> pageUtil = new Page<TbActivity>(query.getPage(), query.getLimit());
        Page<TbActivity> page = tbActivityServices.queryPageList(pageUtil, query);

        Page<TbActivityDto> activityDtoPage = new Page<>();
        List<TbActivityDto> tbActivityDtoList = new ArrayList<>();
        EntityDtoConverter edc = new EntityDtoConverter();
        for (int i = 0; i < page.getRecords().size(); i++) {
            //排除已经结束的活动
            Date now = new Date();
            TbActivity tbActivity = page.getRecords().get(i);
            if (tbActivity.getStarttime().after(now)||tbActivity.getEndtime().before(now)){
                continue;
            }
            TbActivityDto tbActivityDto = new TbActivityDto();
            try {
                tbActivityDto = (TbActivityDto) edc.entityConvertDto(tbActivity, tbActivityDto);
                List<TbActivityCommodity> tbActivityCommodityList = tbActivityCommodityDao.foundByActivityId(tbActivityDto.getId());
                List<TbActivityCommodityDto> tbActivityCommodityDtoList = new ArrayList<>();
                for (int o = 0; o < tbActivityCommodityList.size(); o++) {
                    TbActivityCommodityDto tbActivityCommodityDto = new TbActivityCommodityDto();
                    tbActivityCommodityDto = (TbActivityCommodityDto) edc.entityConvertDto(tbActivityCommodityList.get(o), tbActivityCommodityDto);

                    TbCommodity tbCommodity = tbCommodityService.queryObject(tbActivityCommodityList.get(o).getCommodiytid());
                    TbCommodityDto tbCommodityDto = new TbCommodityDto();
                    tbCommodityDto = (TbCommodityDto) edc.entityConvertDto(tbCommodity, tbCommodityDto);
                    Map<String, Object> map = new HashMap<>();
                    map.put("commodityId", tbCommodityDto.getId());
                    List<TbCommodityProperty> tbCommodityPropertyList = tbCommodityPropertyService.findInfo(map);
                    List<TbProperty> tbPropertyList = new ArrayList<>();
                    for (int k = 0; k < tbCommodityPropertyList.size(); k++) {
                        tbPropertyList.add(tbPropertyService.queryObject(tbCommodityPropertyList.get(k).getPropertyid()));
                    }
                    tbCommodityDto.setTbPropertyList(tbPropertyList);
                    tbActivityCommodityDto.setTbCommodityDto(tbCommodityDto);
                    tbActivityCommodityDtoList.add(tbActivityCommodityDto);
                }

                tbActivityDto.setTbActivityCommodityList(tbActivityCommodityDtoList);

                for (int j = 0; j < tbActivityCommodityList.size(); j++) {
                    TbCommodity tbCommodity = tbCommodityService.queryObject(tbActivityCommodityList.get(j).getCommodiytid());
                    TbCommodityDto tbCommodityDto = new TbCommodityDto();
                    tbCommodityDto = (TbCommodityDto) edc.entityConvertDto(tbCommodity, tbCommodityDto);
                    Map<String, Object> map = new HashMap<>();
                    map.put("commodityId", tbCommodityDto.getId());
                    List<TbCommodityProperty> tbCommodityPropertyList = tbCommodityPropertyService.findInfo(map);
                    List<TbProperty> tbPropertyList = new ArrayList<>();
                    for (int k = 0; k < tbCommodityPropertyList.size(); k++) {
                        tbPropertyList.add(tbPropertyService.queryObject(tbCommodityPropertyList.get(k).getPropertyid()));
                    }
                    tbCommodityDto.setTbPropertyList(tbPropertyList);
                    tbActivityDto.setTbCommodityDto(tbCommodityDto);
                }
                tbActivityDtoList.add(tbActivityDto);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        activityDtoPage.setRecords(tbActivityDtoList);
        return R.ok().put("data", activityDtoPage);
    }

    /**
     * 查询用户参与的活动
     */
    @RequestMapping("/acticityUser")
    public R update(@LoginUserApi TbUser tbUser, int type) {
        List<TbActivityUser> tbActivityUserList = tbActivityUserDao.findbyUid(tbUser.getUserId());
        List<TbActivity> tbActivityList = new ArrayList<>();
        for (int i = 0; i < tbActivityUserList.size(); i++) {
            TbActivity tbActivity = tbActivityServices.selectByPrimaryKey(tbActivityUserList.get(i).getId());
            if (tbActivity.getType() == type) {
                tbActivityList.add(tbActivity);
            }
        }
        return R.ok().put("data", tbActivityList);
    }

    /**
     * 信息
     */
    @AuthIgnoreApi
    @RequestMapping("/info")
    public R info(int id) {
        TbActivity tbActivity = tbActivityServices.selectByPrimaryKey(id);
        TbActivityDto tbActivityDto = new TbActivityDto();
        EntityDtoConverter edc = new EntityDtoConverter();
        try {
            tbActivityDto = (TbActivityDto) edc.entityConvertDto(tbActivity, tbActivityDto);
            List<TbActivityCommodity> tbActivityCommodityList = tbActivityCommodityDao.foundByActivityId(tbActivityDto.getId());
            List<TbActivityCommodityDto> tbActivityCommodityDtoList = new ArrayList<>();
            for (int o = 0; o < tbActivityCommodityList.size(); o++) {
                TbActivityCommodityDto tbActivityCommodityDto = new TbActivityCommodityDto();
                tbActivityCommodityDto = (TbActivityCommodityDto) edc.entityConvertDto(tbActivityCommodityList.get(o), tbActivityCommodityDto);

                TbCommodity tbCommodity = tbCommodityService.queryObject(tbActivityCommodityList.get(o).getCommodiytid());
                TbCommodityDto tbCommodityDto = new TbCommodityDto();
                tbCommodityDto = (TbCommodityDto) edc.entityConvertDto(tbCommodity, tbCommodityDto);
                Map<String, Object> map = new HashMap<>();
                map.put("commodityId", tbCommodityDto.getId());
                List<TbCommodityProperty> tbCommodityPropertyList = tbCommodityPropertyService.findInfo(map);
                List<TbProperty> tbPropertyList = new ArrayList<>();
                for (int k = 0; k < tbCommodityPropertyList.size(); k++) {
                    tbPropertyList.add(tbPropertyService.queryObject(tbCommodityPropertyList.get(k).getPropertyid()));
                }
                tbCommodityDto.setTbPropertyList(tbPropertyList);
                tbActivityCommodityDto.setTbCommodityDto(tbCommodityDto);
                tbActivityCommodityDtoList.add(tbActivityCommodityDto);
            }

            tbActivityDto.setTbActivityCommodityList(tbActivityCommodityDtoList);

            for (int j = 0; j < tbActivityCommodityList.size(); j++) {
                TbCommodity tbCommodity = tbCommodityService.queryObject(tbActivityCommodityList.get(j).getCommodiytid());
                TbCommodityDto tbCommodityDto = new TbCommodityDto();
                tbCommodityDto = (TbCommodityDto) edc.entityConvertDto(tbCommodity, tbCommodityDto);
                Map<String, Object> map = new HashMap<>();
                map.put("commodityId", tbCommodityDto.getId());
                List<TbCommodityProperty> tbCommodityPropertyList = tbCommodityPropertyService.findInfo(map);
                List<TbProperty> tbPropertyList = new ArrayList<>();
                for (int k = 0; k < tbCommodityPropertyList.size(); k++) {
                    tbPropertyList.add(tbPropertyService.queryObject(tbCommodityPropertyList.get(k).getPropertyid()));
                }
                tbCommodityDto.setTbPropertyList(tbPropertyList);
                tbActivityDto.setTbCommodityDto(tbCommodityDto);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return R.ok().put("data", tbActivityDto);
    }

    /**
     * 修改
     */
    @RequestMapping("/findById")
    public R findById(Integer id) {
        EntityDtoConverter edc = new EntityDtoConverter();
        TbActivityCommodity tbActivityCommodity = tbActivityCommodityDao.selectByPrimaryKey(id);
        TbActivityCommodityDto tbActivityCommodityDto = new TbActivityCommodityDto();
        try {
            tbActivityCommodityDto = (TbActivityCommodityDto) edc.entityConvertDto(tbActivityCommodity, tbActivityCommodityDto);
            TbCommodity tbCommodity = tbCommodityService.queryObject(tbActivityCommodityDto.getCommodiytid());
            TbCommodityDto tbCommodityDto = new TbCommodityDto();
            tbCommodityDto = (TbCommodityDto) edc.entityConvertDto(tbCommodity, tbCommodityDto);
            Map<String, Object> map = new HashMap<>();
            map.put("commodityId", tbCommodityDto.getId());
            List<TbCommodityProperty> tbCommodityPropertyList = tbCommodityPropertyService.findInfo(map);
            List<TbProperty> tbPropertyList = new ArrayList<>();
            for (int k = 0; k < tbCommodityPropertyList.size(); k++) {
                tbPropertyList.add(tbPropertyService.queryObject(tbCommodityPropertyList.get(k).getPropertyid()));
            }
            tbCommodityDto.setTbPropertyList(tbPropertyList);
            tbActivityCommodityDto.setTbCommodityDto(tbCommodityDto);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return R.ok().put("data", tbActivityCommodityDto);
    }

    /**
     * 发起砍价活动
     */
    @RequestMapping(value = "/bargain", method = RequestMethod.POST)
    public R bargain(@LoginUserApi TbUser tbUser, @RequestParam Map<String, Object> params) throws InvocationTargetException, IllegalAccessException {

        //判断是帮砍 还是 发起砍价
        if (params.get("buserid") != null) {
            if(tbUser.getParentid()==null||tbUser.getParentid()==1){
                TbUser parent = tbUserService.foundById(Long.valueOf(tbUser.getParentid()));
                bindParend(tbUser,parent);
            }
            //不为空 帮砍
            return bbargain(Integer.parseInt(params.get("activityid").toString()), Integer.parseInt(params.get("commodityid").toString()), tbUser.getUserId(), Integer.parseInt(params.get("buserid").toString()));
        } else {
            //为空  发起砍价
            //1.查询是否已经发起过砍价
            return bargain(Integer.parseInt(params.get("activityid").toString()), Integer.parseInt(params.get("commodityid").toString()), tbUser.getUserId());
        }


    }

    /**
     * 绑定上下级关系,
     * @param tbUser
     * @param parent
     */
    private void bindParend(TbUser tbUser, TbUser parent) {
        WeiXinMsgUtil.tuanduifensi(parent.getWxopenid(),String.valueOf(tbUser.getUserId()),tbUser.getUsername());
        if (tbUserService.queryById(parent.getParentid()) != null) {
            TbUser user2 = tbUserService.queryById(parent.getParentid());
            WeiXinMsgUtil.tuanduifensi(user2.getWxopenid(),String.valueOf(tbUser.getUserId()),tbUser.getUsername());
            tbUser.setParentid2(user2.getUserId());
            if (tbUserService.queryById(user2.getParentid()) != null) {
                TbUser user3 = tbUserService.queryById(user2.getParentid());
                tbUser.setParentid3(user3.getUserId());
                if (tbUserService.queryById(user3.getParentid()) != null) {
                    TbUser user4 = tbUserService.queryById(user3.getParentid());
                    tbUser.setParentid4(user4.getUserId());
                }
            }
        }
        tbUserService.update(tbUser);
    }

    /**
     * 砍价
     */
    public R bargain(Integer activityid, Integer commodityid, Integer userid) throws InvocationTargetException, IllegalAccessException {

        //2。判断砍价是否发起过砍价
        Map<String, Object> params = new HashMap<>();
        params.put("page", 1);
        params.put("limit", Integer.MAX_VALUE);
        params.put("activityid", activityid);
        params.put("commodityid", commodityid);
        params.put("userid", userid);
        params.put("buserid", userid);
        Query query = new Query(params);
        Page<TbBargain> pageUtil = new Page<TbBargain>(query.getPage(), query.getLimit());
        Page<TbBargain> page = tbBargainService.queryPageList(pageUtil, query);
        if (page.getRecords().size() > 0) {
            return R.error("已经发起过该商品的砍价！");
        }

        //1。查询活动详情
        TbActivity tbActivity = tbActivityServices.selectByPrimaryKey(activityid);
        //查询商品信息
        TbCommodity tbCommodity = tbCommodityService.queryObject(commodityid);
        //查询商品和活动中间表
        TbActivityCommodity tbActivityCommodity = tbActivityCommodityDao.foundByCAndA(commodityid, activityid);

        //记录价格
        float money = tbActivityCommodity.getNowprice();

        if (tbCommodity == null) {
            return R.error("未查询到商品信息！");
        }

        //1。存入砍价记录
        TbBargain tbBargain = new TbBargain();
        //发起砍价
        tbBargain.setIsbargain(0);
        tbBargain.setCreatedate(new Date());
        tbBargain.setActivityid(activityid);
        tbBargain.setCommodityid(commodityid);
        tbBargain.setUserid(userid);
        tbBargain.setBuserid(userid);
        //计算砍价
        float bprice = randomUtil.random(money - money * (tbActivity.getThreebargain() == 0 ? 1 : tbActivity.getThreebargain() / 100));
        tbBargain.setPrice((int) bprice);
        tbBargainService.insert(tbBargain);
        EntityDtoConverter edc = new EntityDtoConverter();
        TbBargainDto tbBargainDto = new TbBargainDto();
        tbBargainDto = (TbBargainDto) edc.entityConvertDto(tbBargain, tbBargainDto);
        tbBargainDto.setTbUser(tbUserService.queryById(userid));

        tbActivityCommodityDao.addStore(activityid,1);
        return R.ok("发起砍价成功！").put("data", tbBargain);
    }

    /**
     * 帮砍价
     *
     * @return
     */
    public R bbargain(Integer activityid, Integer commodityid, Integer userid, Integer buserid) throws InvocationTargetException, IllegalAccessException {

        if (this.ifend(buserid, commodityid, activityid)) {
            return R.error("已不满足砍价条件！");
        }

        Map<String, Object> params = new HashMap<>();
        //不为空 帮砍
        //1.判断是否帮砍过
        //查询列表数据
        params.put("userid", buserid);
        params.put("buserid", userid);
        params.put("page", 1);
        params.put("limit", Integer.MAX_VALUE);
        params.put("activityid", activityid);
        params.put("commodityid", commodityid);
        Query query = new Query(params);
        Page<TbBargain> pageUtil = new Page<TbBargain>(query.getPage(), query.getLimit());
        Page<TbBargain> page = tbBargainService.queryPageList(pageUtil, query);
        //1。存入砍价记录
        TbBargain tbBargain = new TbBargain();
        if (page.getRecords().size() > 0) {
            //记录大于0 说明已经砍过了
            return R.error("您已经砍过了～");
        } else {
            //反之 未砍 则进行砍价
            params.remove("buserid");
            //查询出当前需要砍价的活动是否达到要求
            Query query2 = new Query(params);
            Page<TbBargain> pageUtil2 = new Page<TbBargain>(query2.getPage(), query2.getLimit());
            Page<TbBargain> page2 = tbBargainService.queryPageList(pageUtil2, query2);
            if (page2.getRecords().size() > 0) {

                //1。查询活动详情
                TbActivity tbActivity = tbActivityServices.selectByPrimaryKey(activityid);
                //查询商品和活动中间表
                TbActivityCommodity tbActivityCommodity = tbActivityCommodityDao.foundByCAndA(commodityid, activityid);

                //砍价规则
                float money = tbActivityCommodity.getNowprice();

                float san = money - money * (tbActivity.getThreebargain() == 0 ? 1 : tbActivity.getThreebargain() / 100);
                float liu = money - money * (tbActivity.getSixbargain() == 0 ? 1 : tbActivity.getSixbargain() / 100);
                float shi = money - money * (tbActivity.getTenbargain() == 0 ? 1 : tbActivity.getTenbargain() / 100);
                for (int i = 0; i < page2.getRecords().size(); i++) {
                    san -= page2.getRecords().get(i).getPrice();
                    liu -= page2.getRecords().get(i).getPrice();
                    shi -= page2.getRecords().get(i).getPrice();
                }
                tbBargain.setIsbargain(1);
                tbBargain.setCreatedate(new Date());
                tbBargain.setActivityid(activityid);
                tbBargain.setCommodityid(commodityid);
                tbBargain.setUserid(buserid);
                tbBargain.setBuserid(userid);
                //计算砍价
                float bprice = 0f;
                switch (page2.getRecords().size()) {
                    case 1:
                        bprice = randomUtil.random(san);
                        break;
                    case 2:
                        bprice = san;
                        break;
                    case 3:
                    case 4:
                        bprice = randomUtil.random(liu);
                        break;
                    case 5:
                        bprice = liu;
                        break;
                    case 6:
                    case 7:
                    case 8:
                        bprice = randomUtil.random(shi);
                        break;
                    case 9:
                        bprice = shi;
                        break;
                }
                tbBargain.setPrice((int) bprice);
                tbBargainService.insert(tbBargain);
            }
        }
        EntityDtoConverter edc = new EntityDtoConverter();
        TbBargainDto tbBargainDto = new TbBargainDto();
        tbBargainDto = (TbBargainDto) edc.entityConvertDto(tbBargain, tbBargainDto);
        tbBargainDto.setTbUser(tbUserService.queryById(userid));
        return R.ok("帮好友砍价成功！").put("data", tbBargain);
    }


    /**
     * 列表
     */
    @AuthIgnoreApi
    @RequestMapping("/bargainlist")
    public R bargainlist(@RequestParam Map<String, Object> params) throws
            InvocationTargetException, IllegalAccessException {
        TbUser tbUser = new TbUser();
        if (params.get("token") != null) {
            TbToken tbToken = tokenService.queryByToken(params.get("token").toString());
            tbUser = tbUserService.foundById(tbToken.getUserId());
            params.put("userid", tbUser.getUserId());
        }
        //查询列表数据
        Query query = new Query(params);
        Page<TbBargain> pageUtil = new Page<TbBargain>(query.getPage(), query.getLimit());
        Page<TbBargain> page = tbBargainService.queryPageList(pageUtil, query);
        Page<TbBargainDto> tbBargainDtoPage = new Page<TbBargainDto>(query.getPage(), query.getLimit());
        List<TbBargainDto> tbBargainDtoList = new ArrayList<>();
        for (int i = 0; i < page.getRecords().size(); i++) {
            TbBargainDto tbBargainDto = page.getRecords().get(i).toDto();
            tbBargainDto.setTbUser(tbUserService.foundById(Long.parseLong(page.getRecords().get(i).getBuserid().toString())));
            tbBargainDto.setTbCommodity(tbCommodityService.queryObject(tbBargainDto.getCommodityid()));
            tbBargainDto.setTbActivityCommodity(tbActivityCommodityDao.foundByCAndA(tbBargainDto.getCommodityid(), tbBargainDto.getActivityid()));
            tbBargainDtoList.add(tbBargainDto);
        }
        tbBargainDtoPage.setRecords(tbBargainDtoList);
        tbBargainDtoPage.setCondition(page.getCondition());
        return R.ok().put("data", tbBargainDtoPage);
    }


    public boolean ifend(Integer userid, Integer commodityid, Integer activityid) {
        //获取所有发起的砍价信息
        Map<String, Object> params = new HashMap<>();
        params.put("userid", userid);
        params.put("commodityid", commodityid);
        params.put("activityid", activityid);
        params.put("page", 1);
        params.put("limit", Integer.MAX_VALUE);
        Query query = new Query(params);
        Page<TbBargain> pageUtil = new Page<TbBargain>(query.getPage(), query.getLimit());
        Page<TbBargain> page = tbBargainService.queryPageList(pageUtil, query);
        for (int k = 0; k < page.getRecords().size(); k++) {
            if (page.getRecords().get(k).getBuserid().equals(page.getRecords().get(k).getUserid())) {
                if (DateUtil.DateTime1(page.getRecords().get(k).getCreatedate()).getTime() < new Date().getTime()) {
                    System.out.println("111111111111111111:" + DateUtil.DateTime1(page.getRecords().get(k).getCreatedate()).getTime());
                    System.out.println("222222222222222222:" + new Date().getTime());
                    return true;
                }
            }
            if (page.getRecords().size() >= 10) {
                return true;
            }
        }
        return false;

    }

    /**
     * 获取用户发起的砍价
     *
     * @return
     */
    @RequestMapping("/foundByUserBargain")
    public R foundByUserBargain(@LoginUserApi TbUser tbUser) {

        return R.ok();
    }


    /**
     * 获取进行中和已结束的砍价列表
     *
     * @return
     */
    @RequestMapping("/foundByStatusBargain")
    public R foundByStatusBargain(@LoginUserApi TbUser tbUser) {

        return R.ok();
    }

    /**
     * 按type查询砍价列表
     * type = 0 发起的
     * type = 1 结束的
     *
     * @return
     */
    @RequestMapping("/initiate")
    public R initiate(@LoginUserApi TbUser tbUser, @RequestParam Map<String, Object> params) throws
            InvocationTargetException, IllegalAccessException {
        if (params.get("type").equals("0")) {
            params.put("userid", tbUser.getUserId());
            params.put("buserid", tbUser.getUserId());
            Query query = new Query(params);
            Page<TbBargain> pageUtil = new Page<TbBargain>(query.getPage(), query.getLimit());
            Page<TbBargain> page = tbBargainService.queryPageList(pageUtil, query);
            for (int i = 0; i < page.getRecords().size(); i++) {
                TbActivityCommodity tbActivityCommodity = tbActivityCommodityDao.foundByCAndA(page.getRecords().get(i).getCommodityid(), page.getRecords().get(i).getActivityid());
                if(tbActivityCommodity==null) continue;
                //砍价规则
                float money = tbActivityCommodity.getNowprice();
                params.remove("buserid");
                params.put("activityid", tbActivityCommodity.getActivityid());
                params.put("commodityid", tbActivityCommodity.getCommodiytid());
                Query query2 = new Query(params);
                Page<TbBargain> pageUtil2 = new Page<TbBargain>(query2.getPage(), query2.getLimit());
                Page<TbBargain> page2 = tbBargainService.queryPageList(pageUtil2, query2);
                int price = 0;
                for (int k = 0; k < page2.getRecords().size(); k++) {
                    money -= page2.getRecords().get(k).getPrice();
                }
                page.getRecords().get(i).setPrice((int) money);
            }
            Page<TbBargainDto> tbBargainDtoPage = new Page<TbBargainDto>(query.getPage(), query.getLimit());
            List<TbBargainDto> tbBargainDtoList = new ArrayList<>();
            for (int i = 0; i < page.getRecords().size(); i++) {
                EntityDtoConverter edc = new EntityDtoConverter();
                TbBargainDto tbBargainDto = new TbBargainDto();
                tbBargainDto = (TbBargainDto) edc.entityConvertDto(page.getRecords().get(i), tbBargainDto);
                tbBargainDto.setTbUser(tbUserService.foundById(Long.parseLong(page.getRecords().get(i).getBuserid().toString())));
                tbBargainDto.setTbCommodity(tbCommodityService.queryObject(tbBargainDto.getCommodityid()));
                tbBargainDto.setTbActivityCommodity(tbActivityCommodityDao.foundByCAndA(tbBargainDto.getCommodityid(), tbBargainDto.getActivityid()));
                tbBargainDto.setTbActivity(tbActivityServices.selectByPrimaryKey(tbBargainDto.getActivityid()));

                if (DateUtil.DateTime1(page.getRecords().get(i).getCreatedate()).getTime() > new Date().getTime()) {
                    if (page.getRecords().get(i).getBuserid().equals(page.getRecords().get(i).getUserid())) {
                        tbBargainDtoList.add(tbBargainDto);
                    }
                }

            }
            tbBargainDtoPage.setRecords(tbBargainDtoList);
            tbBargainDtoPage.setCondition(page.getCondition());
            return R.ok().put("data", tbBargainDtoPage);
        } else {
            params.put("userid", tbUser.getUserId());
            Query query = new Query(params);
            Page<TbBargain> pageUtil = new Page<TbBargain>(query.getPage(), query.getLimit());
            Page<TbBargain> page = tbBargainService.queryPageList(pageUtil, query);
            for (int i = 0; i < page.getRecords().size(); i++) {
                TbActivityCommodity tbActivityCommodity = tbActivityCommodityDao.foundByCAndA(page.getRecords().get(i).getCommodityid(), page.getRecords().get(i).getActivityid());
                //砍价规则
                if (tbActivityCommodity==null)continue;
                float money = tbActivityCommodity.getNowprice();
                params.put("activityid", tbActivityCommodity.getActivityid());
                params.put("commodityid", tbActivityCommodity.getCommodiytid());
                Query query2 = new Query(params);
                Page<TbBargain> pageUtil2 = new Page<TbBargain>(query2.getPage(), query2.getLimit());
                Page<TbBargain> page2 = tbBargainService.queryPageList(pageUtil2, query2);
                int price = 0;
                for (int k = 0; k < page2.getRecords().size(); k++) {
                    money -= page2.getRecords().get(k).getPrice();
                }
                page.getRecords().get(i).setPrice((int) money);
            }
            Page<TbBargainDto> tbBargainDtoPage = new Page<TbBargainDto>(query.getPage(), query.getLimit());
            List<TbBargainDto> tbBargainDtoList = new ArrayList<>();
            for (int i = 0; i < page.getRecords().size(); i++) {
                EntityDtoConverter edc = new EntityDtoConverter();
                TbBargainDto tbBargainDto = new TbBargainDto();
                tbBargainDto = (TbBargainDto) edc.entityConvertDto(page.getRecords().get(i), tbBargainDto);
                tbBargainDto.setTbUser(tbUserService.foundById(Long.parseLong(page.getRecords().get(i).getBuserid().toString())));
                tbBargainDto.setTbCommodity(tbCommodityService.queryObject(tbBargainDto.getCommodityid()));
                tbBargainDto.setTbActivityCommodity(tbActivityCommodityDao.foundByCAndA(tbBargainDto.getCommodityid(), tbBargainDto.getActivityid()));
                tbBargainDto.setTbActivity(tbActivityServices.selectByPrimaryKey(tbBargainDto.getActivityid()));

                if (DateUtil.DateTime1(page.getRecords().get(i).getCreatedate()).getTime() < new Date().getTime() || page.getRecords().size() >= 10) {
                    if (page.getRecords().get(i).getBuserid().equals(page.getRecords().get(i).getUserid())) {
                        tbBargainDtoList.add(tbBargainDto);
                    }
                }

            }
            tbBargainDtoPage.setRecords(tbBargainDtoList);
            tbBargainDtoPage.setCondition(page.getCondition());
            return R.ok().put("data", tbBargainDtoPage);
        }

    }

}
